import $http from "@/common/util/http";
import $router, {asyncAddRoute} from '@/common/router/router'

import {useRouteStore} from "@/common/store/modules/route";
import {useUserStore} from "@/common/store/modules/user";
import {Md5} from "ts-md5";
import {$event} from "@/common/util/event";
import {useLayoutStore} from "@/common/store/modules/layout";
import {removeRefreshToken, removeToken, setRefreshToken, setToken} from "@/common/util/cache";
import {parseTree} from "@/common/util/tree";

//查询当前登录用户菜单
export async function refreshMenu() {
    return new Promise((resolve, reject) => {
        $http.post('/system/user/menu', {})
            .then(async (res: any) => {

                //缓存全部菜单
                let menu = res.data;
                //缓存菜单树
                let menuTree = parseTree(res.data);
                //缓存-保活组件路径
                let cachePaths = []
                res.data.filter(e => {
                    if (e.cache_status == '0') {
                        cachePaths.push(e.path)
                    }
                })

                //
                useRouteStore().menu = menu;
                useRouteStore().menuTree = menuTree;
                useRouteStore().cacheMenuPaths = cachePaths;

                //
                $event.$emit('refreshPan')

                //加载异步菜单
                await asyncAddRoute(useRouteStore().menu)
                // router.addRoutes(accessRoutes) // 动态添加可访问路由表
                // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成

                //console.log("【菜单集合】", useRouteStore().menu)
                //console.log("【菜单树形结构】", useRouteStore().menuTree)
                //console.log("【菜单保活】", useRouteStore().cacheMenuPaths)
                resolve(true)
            })
            .catch((e) => {
                resolve(false)
            })
    })

}

//查询当前登录用户信息
export async function refreshUserInfo() {
    return new Promise((resolve, reject) => {
        $http.post('/system/user/loginUserInfo', {})
            .then((res: any) => {
                useUserStore().userInfo = res.data;
                resolve(true)
            })
            .catch((e) => {
                useUserStore().userInfo = null;
                resolve(false)
            })

    })
}

//登录方法
export async function systemLogin(account, password) {
    return new Promise((resolve, reject) => {

        let saltPassword = Md5.hashStr(account + password)

        let data = {
            account: account,
            password: saltPassword.toUpperCase(),
            type: 0
        }

        //登录
        $http.post('/system/user/login', data)
            .then(async (res: any) => {
                setToken(res.data.token)
                setRefreshToken(res.data.refreshToken)
                resolve(true)
            })
            .catch((e) => {
                resolve(false)
            })
    })

}

//退出登录
export async function systemLogOut(message = '') {
    return new Promise((resolve, reject) => {
        $http.post('/system/user/logout', {message})
            .then(async (res: any) => {
                removeToken()
                removeRefreshToken()
                useRouteStore().menu = [];
                useRouteStore().menuTree = [];
                useRouteStore().cacheMenuPaths = [];
                useLayoutStore().tabView = [];
                useUserStore().isLogOut = false
                // window.location.reload();
                await $router.push("/login")
                resolve(true)
            })
    })
}
